
package com.zhu.salesmanagement.views;

import com.dms2.salesmanagement.R;
import com.zhu.salesmanagement.CRCInfoActivity;
import com.zhu.salesmanagement.DealerTaskListActivity;
import com.zhu.salesmanagement.StoreEvaluationActivity;
import com.zhu.salesmanagement.StoreGuideInfoActivity;
import com.zhu.salesmanagement.StoreScMsgActivity;
import com.zhu.salesmanagement.VisitTaskListActivity;
import com.zhu.salesmanagement.common.Actions;
import com.zhu.salesmanagement.models.CEvaluationItem;
import com.zhu.salesmanagement.models.VisitTaskInfo;
import com.zhu.salesmanagement.provider.SalesSettings;
import com.zhu.salesmanagement.utils.Util;

import android.content.AsyncQueryHandler;
import android.content.ContentResolver;
import android.content.ContentValues;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.util.AttributeSet;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.ScrollView;
import android.widget.TextView;

/**
 * 门店信息
 */
public class StoreDataView extends LinearLayout implements View.OnClickListener{

    private final static String TAG = "zhu";
    private boolean isInited = false;

    private TextView mYewuTextView = null;
    private TextView mWangdianTextView = null;
    private TextView mNearCRCTextView = null;
    private TextView mNearSDTextView = null;
    private StoreEvaluationActivity mStoreEvaluationActivity = null;

    public StoreDataView(Context context) {
        super(context);
        // TODO Auto-generated constructor stub
    }

    public StoreDataView(Context context, AttributeSet attrs) {
        super(context, attrs);
        // TODO Auto-generated constructor stub
    }

    @Override
    public void onFinishInflate() {
        setupViews();
        super.onFinishInflate();
    }

    /**
     * 初始化界面
     */
    private void setupViews() {
        mYewuTextView = (TextView)this.findViewById(R.id.sd_yewuText);
        mWangdianTextView = (TextView)this.findViewById(R.id.sd_wangdianText);
        mNearCRCTextView = (TextView)this.findViewById(R.id.sd_nearcrcText);
        mNearSDTextView = (TextView)this.findViewById(R.id.sd_nearstoredataText);

        findViewById(R.id.sd_daogou).setOnClickListener(this);
        findViewById(R.id.sd_crc).setOnClickListener(this);
        findViewById(R.id.sd_zhongyao).setOnClickListener(this);
        findViewById(R.id.sd_cuxiao).setOnClickListener(this);
    }

    /**
     * 初始化数据
     * @param activity Activity
     */
    public void initData(StoreEvaluationActivity activity) {
        mStoreEvaluationActivity = activity;
        if (isInited) {
            return;
        }
        isInited = true;
        updateViewFromCursor();
    }

    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub
        switch(v.getId()){
            case R.id.sd_daogou:
                gotoDaogou();
                break;
            case R.id.sd_crc:
                gotoCRCInfo();
                break;
            case R.id.sd_zhongyao:
                gotoSCImportMsg();
                break;
            case R.id.sd_cuxiao:
                Actions.toastShort(getContext(), getContext().getString(R.string.noactioninfo));
                break;
        }
    }

    /**
     * 跳转导购
     */
    private void gotoDaogou(){
    	 Intent intent = new Intent(getContext(), StoreGuideInfoActivity.class);
    	 intent.putExtra("cid", mStoreEvaluationActivity.getVisitTaskInfo().id);
         getContext().startActivity(intent);
    }

    /**
     * 跳转重要讯息
     */
    private void gotoSCImportMsg(){
        Intent intent = new Intent(getContext(), StoreScMsgActivity.class);
        intent.putExtra(StoreScMsgActivity.CUSTOMERID, mStoreEvaluationActivity.getVisitTaskInfo().id);
        intent.putExtra(StoreScMsgActivity.IMSG, mStoreEvaluationActivity.getVisitTaskInfo().sMsg);
        getContext().startActivity(intent);
   }

    /**
     * 产看CRC
     */
    private void gotoCRCInfo(){
        Intent intent = new Intent(getContext(), CRCInfoActivity.class);
        Bundle bundle = new Bundle();
        bundle.putParcelable("info", mStoreEvaluationActivity.getVisitTaskInfo());
        intent.putExtras(bundle);
        getContext().startActivity(intent);
    }

    /**
     * 更新界面
     */
    private void updateViewFromCursor(){
//        String where = SalesSettings.CustomerSColumns.CUSTOMER_ID + " =\'" + mStoreEvaluationActivity.getVisitTaskInfo().id +" \'";
//        String name = null;
//        String type = null;
//        String crc = null;
//        String sc = null;
//        Cursor cursor = getContext().getContentResolver().query(SalesSettings.CustomerSColumns.CONTENT_URI, null, where, null, null);
//        if(cursor != null && cursor.moveToFirst()){
//            name = cursor.getString(cursor.getColumnIndexOrThrow(SalesSettings.CustomerSColumns.CUSTOMER_DEALERNAME));
//            type = cursor.getString(cursor.getColumnIndexOrThrow(SalesSettings.CustomerSColumns.CUSTOMER_SCATEGORT));
//            crc = cursor.getString(cursor.getColumnIndexOrThrow(SalesSettings.CustomerSColumns.CUSTOMER_CRCNEARTIME));
//            sc = cursor.getString(cursor.getColumnIndexOrThrow(SalesSettings.CustomerSColumns.CUSTOMER_SCNEARTIME));
//        }
//        if(cursor != null){
//            cursor.close();
//            cursor = null;
//        }
        VisitTaskInfo info = mStoreEvaluationActivity.getVisitTaskInfo();
        mYewuTextView.setText(info.sDealtSalesPerson);
        mWangdianTextView.setText(info.sStoreCategory);
        mNearCRCTextView.setText(info.sCRCNearTime);
        mNearSDTextView.setText(info.sSCNearTime);
    }
}
